package com.gxuwz.dao.impl;

import com.gxuwz.dao.LeaveDao;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.Leave;
import com.gxuwz.entity.dto.LeaveDto;
import com.gxuwz.entity.vo.LeaveVo;
import com.gxuwz.entity.Classes;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.Leave;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class LeaveDaoImpl implements LeaveDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public PageVo selectLeaveByInstID(LeaveDto leaveDto, String instID, PageInfo pageInfo) {
        String sql = "select leaveID,sys_leave.stuID,stuName,courseName,applyTime,dayNum,reason,status,auditTime,opinion " +
                "from sys_leave,sys_course,sys_student,sys_classes " +
                "where sys_leave.stuID = sys_student.stuID and sys_leave.courseID = sys_course.courseID " +
                "and sys_classes.classID = sys_student.classID and sys_leave.del_flag = 0 and " +
                "sys_course.del_flag = 0 and sys_classes.del_flag = 0 and sys_student.del_flag = 0 and " +
                "sys_leave.instID = ? ";
        BeanPropertyRowMapper<LeaveVo> propertyRowMapper = new BeanPropertyRowMapper<LeaveVo>(LeaveVo.class);
        PageUtil pageUtil = new PageUtil();
        List<Object> example = new ArrayList<Object>();
        if (leaveDto == null) {
            Object[] params = new Object[]{instID};
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, propertyRowMapper, jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            example.add(instID);
            if (leaveDto.getLeaveID() != null && !leaveDto.getLeaveID().equals("")) {
                sql += "and leaveID like ?";
                example.add("%" + leaveDto.getInstID() + "%");
            }
            if (leaveDto.getClassID() != null && !leaveDto.getClassID().equals("")) {
                sql += "and sys_classes.classID like ?";
                example.add("%" + leaveDto.getClassID() + "%");
            }
            if (leaveDto.getStuID() != null && !leaveDto.getStuID().equals("")) {
                sql += "and sys_leave.stuID like ?";
                example.add("%" + leaveDto.getStuID() + "%");
            }
            if (leaveDto.getStatusLeave() != null && !leaveDto.getStatusLeave().equals("")) {
                sql += "and status = ?";
                example.add(leaveDto.getStatusLeave());
            }
            params = example.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, propertyRowMapper, jdbcTemplate);
            return pageVo;
        }

    }

    public int updateLeaveStatus(Leave entity) {
        String sql = "update sys_leave set status = ?,auditTime = ?,opinion = ? where leaveID = ?";
        Object[] params = new Object[]{entity.getStatus(),entity.getAuditTime(),entity.getOpinion(),entity.getLeaveID()};
        int update = jdbcTemplate.update(sql, params);
        return update;
    }

    public List<LeaveVo> selectLeaveByIds(String[] ids) {
        return null;
    }


    public LeaveVo selectLeaveByID(String leaveID) {
        String sql = "select leaveID,sys_leave.stuID,sys_course.courseID,stuName,courseName,applyTime,dayNum,reason,status,auditTime,opinion " +
                "from sys_leave,sys_course,sys_student,sys_classes " +
                "where sys_leave.stuID = sys_student.stuID and sys_leave.courseID = sys_course.courseID " +
                "and sys_classes.classID = sys_student.classID and sys_leave.del_flag = 0 and " +
                "sys_course.del_flag = 0 and sys_student.del_flag = 0 and sys_classes.del_flag = 0 and " +
                "sys_leave.leaveID = ? ";
        Object[] params = new Object[]{leaveID};
        BeanPropertyRowMapper<LeaveVo> leaveVoBeanPropertyRowMapper = new BeanPropertyRowMapper<>(LeaveVo.class);
        LeaveVo leaveVo = jdbcTemplate.queryForObject(sql, leaveVoBeanPropertyRowMapper, params);
        return leaveVo;
    }

    public PageVo selectLeaveAll(LeaveDto entity, PageInfo pageInfo,String stuID) {
        String sql = "select leaveID,courseName,reason,dayNum,status from sys_leave,sys_course " +
                "where sys_leave.courseID = sys_course.courseID and sys_leave.del_flag = 0 " +
                "and sys_course.del_flag = 0 and sys_leave.stuID = ?";
        BeanPropertyRowMapper<LeaveVo> leaveBeanPropertyRowMapper = new BeanPropertyRowMapper<LeaveVo>(LeaveVo.class);
        List<Object> list = new ArrayList<Object>();
        PageUtil pageUtil = new PageUtil();
        if(entity==null){
            Object[] params = new Object[]{stuID};
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, leaveBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        } else {
            Object[] params;
            list.add(stuID);
            if(entity.getCourseName() != null && !entity.getCourseName().equals("")){
                sql += "and courseName like ?";
                list.add("%"+entity.getCourseName()+"%");
            }
            params = list.toArray();
            PageVo pageVo = pageUtil.queryForPage(sql, pageInfo.getPageNum(), pageInfo.getPageSize(), params, leaveBeanPropertyRowMapper,jdbcTemplate);
            return pageVo;
        }
    }

    public LeaveVo selectLeaveByLeaveID(String leaveId) {
        String sql = "select leaveID,courseName,dayNum,reason,reason,opinion from sys_leave,sys_course " +
                "where leaveID = ? and sys_leave.courseID = sys_course.courseID and sys_leave.del_flag = 0 " +
                "and sys_course.del_flag = 0 ";
        BeanPropertyRowMapper<LeaveVo> leaveBeanPropertyRowMapper = new BeanPropertyRowMapper<LeaveVo>(LeaveVo.class);
        Object[] params = new Object[]{leaveId};
        try{
            LeaveVo leaveVo = jdbcTemplate.queryForObject(sql, leaveBeanPropertyRowMapper, params);
            return leaveVo;
        }catch (Exception e){
            return null;
        }
    }

    public int insertLeave(Leave entity) {
        String sql = "insert into sys_leave(leaveID,courseID,reason,dayNum,stuID,applyTime,status,instID,del_flag) " +
                "values(?,?,?,?,?,?,?,?,0)";
        Object[] params = new Object[]{entity.getLeaveID(),entity.getCourseID(),entity.getReason(), entity.getDayNum(),entity.getStuNo(),
                entity.getApplyTime(),entity.getStatus(),entity.getInstID()};
        int insert = jdbcTemplate.update(sql,params);
        return insert;
    }

    public int deleteLeaveById(String leaveId) {
        String sql = "update sys_leave set del_flag = 1 where leaveID = ?";
        Object[] params = new Object[]{leaveId};
        int delete = jdbcTemplate.update(sql,params);
        return delete;
    }

    public String getInstIDByStuID(String stuID){
        String sql = "select instID from sys_classes where classID IN(" +
                "select classID from sys_student where stuID = ? " +
                "and sys_student.del_flag = 0 ) and sys_classes.del_flag = 0 ";
        Object[] params = new Object[]{stuID};
        BeanPropertyRowMapper<Classes> classesBeanPropertyRowMapper = new BeanPropertyRowMapper<Classes>(Classes.class);
        try{
            Classes classes = jdbcTemplate.queryForObject(sql,classesBeanPropertyRowMapper,params);
            return classes.getInstID();
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<LeaveVo> selectLeaveListByInst(LeaveDto leaveDto, String instID) {
        String sql = "select leaveID,sys_leave.stuID,stuName,courseName,applyTime,dayNum,reason,status,auditTime,opinion " +
                "from sys_leave,sys_course,sys_student,sys_classes " +
                "where sys_leave.stuID = sys_student.stuID and sys_leave.courseID = sys_course.courseID " +
                "and sys_classes.classID = sys_student.classID and sys_leave.status <> 0 and sys_leave.del_flag = 0 and " +
                "sys_course.del_flag = 0 and sys_classes.del_flag = 0 and sys_student.del_flag = 0 and " +
                "sys_leave.instID = ? ";
        BeanPropertyRowMapper<LeaveVo> propertyRowMapper = new BeanPropertyRowMapper<LeaveVo>(LeaveVo.class);
        List<Object> example = new ArrayList<Object>();
        if (leaveDto == null) {
            Object[] params = new Object[]{instID};
            List<LeaveVo> query = jdbcTemplate.query(sql, propertyRowMapper, params);
            return query;
        } else {
            Object[] params;
            example.add(instID);
            if (leaveDto.getLeaveID() != null && !leaveDto.getLeaveID().equals("")) {
                sql += "and leaveID like ?";
                example.add("%" + leaveDto.getInstID() + "%");
            }
            if (leaveDto.getClassID() != null && !leaveDto.getClassID().equals("")) {
                sql += "and sys_classes.classID like ?";
                example.add("%" + leaveDto.getClassID() + "%");
            }
            if (leaveDto.getStuID() != null && !leaveDto.getStuID().equals("")) {
                sql += "and sys_leave.stuID like ?";
                example.add("%" + leaveDto.getStuID() + "%");
            }
            if (leaveDto.getStatusLeave() != null && !leaveDto.getStatusLeave().equals("")) {
                sql += "and status = ?";
                example.add(leaveDto.getStatusLeave());
            }
            params = example.toArray();
            List<LeaveVo> query = jdbcTemplate.query(sql, propertyRowMapper, params);
            return query;
        }
    }
}

